<?php



/**

 * user actions.

 *

 * @package    mocean_sms

 * @subpackage user

 * @author     Your name here

 * @version    SVN: $Id: actions.class.php 500 2006-01-23 09:15:57Z fabien $

 */

class userActions extends sfActions

{

  /**

   * Executes index action

   *

   */

  public function executeLogin()
  {
  	$_SESSION['user_id']="";
  	$this->error_message="";
  	
  	if ($this->getRequest()->getMethod() != sfRequest::POST) {
		$this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	} else {
      	// handle the form submission
      	$username = $this->getRequestParameter('username');
	  	$password = $this->getRequestParameter('password');
	  	
      	$c = new Criteria();
      	$c->add(TblUserPeer::F_USERNAME, $username);
		$c->addAnd(TblUserPeer::F_PASSWORD, $password);
		$c->addAnd(TblUserPeer::F_STATUS, "active");
	  	$tbl_user = TblUserPeer::doSelectOne($c);

      	if ($tbl_user) {			
			$this->getUser()->setAuthenticated(true);
   	      	$this->getUser()->addCredential('user');
			$this->getUser()->setAttribute('user_id', $tbl_user->getFId(), 'user');
   	  		$this->getUser()->setAttribute('user_name', $tbl_user->getFUsername(), 'user');

    	  	$_SESSION['user_id']=$tbl_user->getFId();
		 	$_SESSION['user_name']=$tbl_user->getFUsername();
				
			return $this->redirect('user/main');
		} else {
			$this->error_message='The username and password entered does not match';
		} 
   	 }
  }
  
  public function executeLogout()
  {
    $this->getUser()->setAuthenticated(false);
    $this->getUser()->clearCredentials();
    $this->getUser()->getAttributeHolder()->removeNamespace('user');
	session_unset();
    $this->redirect('@homepage');
  }
  
  public function executeMain(){
  	
  }
  
  public function executeForgetpass()
  {
    $this->error_message = "";
    $this->success_message = "";
     
    if ($this->getRequest()->getMethod() != sfRequest::POST) {
		$this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	} else {
      	// handle the form submission
      	$username = $this->getRequestParameter('username');
	  	$email = $this->getRequestParameter('email');
    
    	$c = new Criteria();
      	$c->add(TblUserPeer::F_USERNAME, $username);
      	$c->addAnd(TblUserPeer::F_EMAIL, $email);
	  	$tbl_user = TblUserPeer::doSelectOne($c);
	  	
	  	if($tbl_user){
	  		$random_number = rand(10000000,99999999);
	  		/*
	  		include_once('class.phpmailer.php');
			$mail = new PHPMailer();
			$mail->IsSMTP(); // set mailer to use SMTP
			$mail->Host = "mail.bigtreeoutdoor.com"; // specify main and backup server
			$mail->SMTPAuth = true; // turn on SMTP authentication
			$mail->Username = "sendemail@bigtreeoutdoor.com"; // SMTP username
			$mail->Password = "abc123"; // Please enter the SMTP password for the user name
			$mail->IsHTML(true);
			$mail->From = "sendemail@bigtreeoutdoor.com";
			$mail->FromName = "BIG PICTURE";	
			$mail->Subject = "Reset Password";
			
			$body .= "New Password:".$random_number;
	
			$mail->Body    = $body;
			$mail->AddAddress($tbl_user->getFEmail(), $tbl_user->getFName());
			*/
			
			include("Mail.php");
			$headers["From"]= "BIG PICTURE <sendemail@bigtreeoutdoor.com>";
			$headers["To"]= $tbl_user->getFEmail();
			$headers["Subject"]="BIG PICTURE - Account Password Reset";
			$headers["Content-Type"]="text/html; charset=iso-8859-1";
			
			$params["host"]="mail.bigtreeoutdoor.com";
			$params["port"]="25";
			$params["auth"]=true;
			$params["username"]="sendemail@bigtreeoutdoor.com";
			$params["password"]="abc123";
			$url = "http://ecoc.bigtreeoutdoor.com";
			$body ="<html><head>";
			$body.="<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
			$body.="</head>";
			$body.="<body>";
			$body.="You had reset your password on ".date('d-m-Y H:i')."<br><br>";
			$body.="New Password: ".$random_number."<br><br>";
			$body.="To login, click the link below or copy to your browser.<br>";
			$body.="<a href='".$url."'>".$url."</a><br><br>";
			$body.="To change the password, please click the link below or copy the link below to your browser.<br><br>";
			$url = "http://ecoc.bigtreeoutdoor.com/user/changepass";
			$body.="<a href='".$url."'>".$url."</a><br><br>";
			$body.="Thank You.";
			$body.="</body>";
			$body.="</html>";
						
			$mail_object=&Mail::factory("smtp",$params);
			$result = $mail_object->send($tbl_user->getFEmail(),$headers,$body);
				
			if(!$result) {
				$this->error_message = "Unable to send email, please try again";
			}else{
				$tbl_user->setFPassword(md5($random_number));
				$tbl_user->save();
				$this->success_message = "success";
			}
	  	}else{
	  		$this->error_message = "Your username and email doesn't match, if you forgot your username and email, please contact the admin";
	  	}     	
  	}
  }
  
  public function executeChangepass()
  {
    $this->error_message = "";
    $this->success_message = "";
     
    if ($this->getRequest()->getMethod() != sfRequest::POST) {
		$this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	} else {
      	// handle the form submission
      	$username = $this->getRequestParameter('username');
	  	$password = $this->getRequestParameter('password');
	  	$password1 = $this->getRequestParameter('password1');
	  	$password2 = $this->getRequestParameter('password2');
    
    	$c = new Criteria();
      	$c->add(TblUserPeer::F_USERNAME, $username);
      	$c->addAnd(TblUserPeer::F_PASSWORD, md5($password));
	  	$tbl_user = TblUserPeer::doSelectOne($c);
	  	
	  	if($tbl_user){
	  		if($tbl_user->getFType()=='Client'){
	  			$this->error_message = "This account password cannot be change";
	  		}else{
		  		$tbl_user->setFPassword(md5($password1));
				$tbl_user->save();
				$this->success_message = "success";				
	  		}
	  	}else{
	  		$this->error_message = "The username and password entered does not match";
	  	}     	
  	}
  }
  
  public function executeList()
  {
  	if($_SESSION['user_id']<=0){
  		$this->redirect('@homepage');
  	}else{
  		
	    $c = new Criteria();
	   	$c->add(TblUserPeer::F_STATUS, "active");
      	$c->addAscendingOrderByColumn(TblUserPeer::F_USERNAME);
	  	$this->tbl_users = TblUserPeer::doSelect($c);
  	}
  }
  
  public function executeEdit ()
  { 
	if($_SESSION['user_id']<=0){
  		$this->redirect('@homepage');
  	}else{
	 	$this->tbl_user = $this->getUserOrCreate();
	 	
	 	$c = new Criteria();
	 	if($this->tbl_user->getFId()>0){
	 		$c->add(TblUserPeer::F_ID, $this->tbl_user->getFId(), Criteria::NOT_EQUAL);
			$c->addAnd(TblUserPeer::F_STATUS, 'active');
	 	}
	 	$tbl_users = TblUserPeer::doSelect($c);
	 	
	 	$this->avail_user = "";
	 	foreach ($tbl_users as $user){
	 		if($this->avail_user=="")
	 			$this->avail_user = $user->getFUsername();
	 		else
	 			$this->avail_user .= ";".$user->getFUsername();
	 	}
	}
  }
  
  public function executeDelete ()
  { 
	if($_SESSION['user_id']<=0){
  		$this->redirect('@homepage');
  	}else{
	 	$tbl_user = $this->getUserOrCreate();
	 	$tbl_user->setFStatus('deleted');
	 	$tbl_user->setFUpdatedDatetime(date('Y-m-d H:i:s'));
		$tbl_user->setFUpdatedUid($_SESSION['user_id']);
	 	$tbl_user->save();
	 	
	 	return $this->redirect('user/list?message=The selected user is deleted!');	 		 	
	}
  }
  
  public function executeUpdate ()
  {    
	$tbl_user = $this->getUserOrCreate();	    
	$new_user = false;
	$c = new Criteria();
	$c->add(TblUserPeer::F_USERNAME, $this->getRequestParameter('f_username'));
	
	if ($tbl_user->getFId()>0){
		$c->add(TblUserPeer::F_ID, $tbl_user->getFId(), CRITERIA::NOT_EQUAL);
	}
	$isuserexist = TblUserPeer::doSelectOne($c);
	if ($isuserexist){
		return $this->redirect('user/list?message=Username have been taken');
	}else{	  	
	  	$tbl_user->setFUsername($this->getRequestParameter('f_username'));
	  	$tbl_user->setFPassword($this->getRequestParameter('f_password'));
	  	$tbl_user->setFStatus("active");
		$tbl_user->save();
		
		return $this->redirect('user/list');
	}	
  }
  
  private function getUserOrCreate ($id = 'f_id')
  {
	if (!$this->getRequestParameter($id, 0))
    {
      $tbl_user = new TblUser();
    }
    else
    {
      $tbl_user = TblUserPeer::retrieveByPk($this->getRequestParameter($id));
      $this->forward404Unless($tbl_user instanceof TblUser);
    }
    return $tbl_user;
  }
  
  public function executeSitelist()
  {
  	if($_SESSION['user_type']!="System"){
  		$this->redirect('@homepage');
  	}else{
  		$c = new Criteria();
	  	$c->add(TblSitePeer::F_DELETE, '0');
	  	if($this->getRequestParameter('cbo_search')>0){
	  		if($this->getRequestParameter('cbo_search')==1)
		  		$c->addAnd(TblSitePeer::F_SITE_NO, '%'.$this->getRequestParameter('txt_search').'%', Criteria::LIKE);
		  	elseif($this->getRequestParameter('cbo_search')==2)
		  		$c->addAnd(TblSitePeer::F_SITE_LOCATION, '%'.$this->getRequestParameter('txt_search').'%', Criteria::LIKE);	  	
		  	elseif($this->getRequestParameter('cbo_search')==3)
		  		$c->addAnd(TblSitePeer::F_DISPLAY_SIZE, '%'.$this->getRequestParameter('txt_search').'%', Criteria::LIKE);
		  	
		  	$this->txt_search = $this->getRequestParameter('txt_search');
		  	$this->cbo_search = $this->getRequestParameter('cbo_search');
		  	$this->condition .= '&cbo_search='.$this->getRequestParameter('cbo_search').'&txt_search='.$this->getRequestParameter('txt_search');
	  	}
	  
	  	if($this->getRequestParameter('cbo_order')=='desc'){
		  	$c->addDescendingOrderByColumn(TblSitePeer::F_SITE_NO);
	  	}else{
	  		$c->addAscendingOrderByColumn(TblSitePeer::F_SITE_NO);
	  	}  	
	  	$this->cbo_order = $this->getRequestParameter('cbo_order');
	  	$this->condition .= '&cbo_order='.$this->getRequestParameter('cbo_order');
	  	 
	 	$pager = new sfPropelPager('TblSite', 20);
		$pager->setCriteria($c);
		$pager->setPage($this->getRequestParameter('page', 1));
		$pager->init();
		$this->pager = $pager;
	  	
	  	$this->message = $this->getRequestParameter('message');
  	}
  }
  
  public function executeEditsite ()
  { 
	if($_SESSION['user_type']!="System"){
  		$this->redirect('@homepage');
  	}else{
	 	$this->tbl_site =  $this->getSiteOrCreate();
	 	
	 	$c = new Criteria();
	 	if($this->tbl_site->getFId()>0){
	 		$c->add(TblSitePeer::F_ID, $this->tbl_site->getFId(), Criteria::NOT_EQUAL);
	 	}
	 	$tbl_sites = TblSitePeer::doSelect($c);
	 	
	 	$this->avail_site = "";
	 	foreach ($tbl_sites as $site){
	 		if($this->avail_site=="")
	 			$this->avail_site = $site->getFSiteNo();
	 		else
	 			$this->avail_site .= ";".$site->getFSiteNo();
	 	}
	}
  }
  
  public function executeUpdatesite ()
  {    
	$tbl_site = $this->getSiteOrCreate();
	$tbl_site->setFSiteNo($this->getRequestParameter('f_site_no'));
	$tbl_site->setFSiteLocation($this->getRequestParameter('f_site_location'));
	$tbl_site->setFDisplaySize($this->getRequestParameter('f_display_size'));    
	$tbl_site->save();
		
	return $this->redirect('user/sitelist');	
  }
  
  private function getSiteOrCreate ($id = 'f_id')
  {
	if (!$this->getRequestParameter($id, 0))
    {
      $tbl_site = new TblSite();
    }
    else
    {
      $tbl_site = TblSitePeer::retrieveByPk($this->getRequestParameter($id));
      $this->forward404Unless($tbl_site instanceof TblSite);
    }
    return $tbl_site;
  }
}
?>